home *** CD-ROM | disk | FTP | other *** search
/ All for Cell Phones: Sony Ericsson / Sony-Ericsson 2004.iso / Java / Eval / eval.jar / eval / Eval.class (.txt) < prev    next >
Encoding:
Java Class File  |  2002-06-13  |  7.2 KB  |  269 lines

  1. package eval;
  2.  
  3. import javax.microedition.lcdui.Alert;
  4. import javax.microedition.lcdui.Command;
  5. import javax.microedition.lcdui.CommandListener;
  6. import javax.microedition.lcdui.Display;
  7. import javax.microedition.lcdui.Displayable;
  8. import javax.microedition.lcdui.Image;
  9. import javax.microedition.lcdui.List;
  10. import javax.microedition.lcdui.TextBox;
  11. import javax.microedition.midlet.MIDlet;
  12. import javax.microedition.rms.RecordStore;
  13. import javax.microedition.rms.RecordStoreException;
  14.  
  15. public class Eval extends MIDlet implements CommandListener {
  16.    public static final int VERSION = 1026;
  17.    public static final int MAX_LEN = 512;
  18.    public static int PRECISION = 10;
  19.    public static int HISTORY_SIZE = 10;
  20.    private static final String ABOUT = "Supported operators are: + - / * ( ) =, functions: sin(), cos(), constants: Pi.\nTo store result in a variable use '=' operator, for example: var1=sin(pi/4)*sin(pi/4)+cos(pi/4)*cos(pi/4)\nMidlet developed by WAP INDUSTRIAL [support@wapindustrial.com]. We can develop customized midlets for you!";
  21.    private static final String ITEM0 = "\n=press the SELECT button=";
  22.    private Display display;
  23.    private List hist;
  24.    private TextBox entry;
  25.    private TextBox prec;
  26.    private Alert directions = new Alert("Help");
  27.    private int histIndex = 0;
  28.    private Command exitCommand = new Command("Exit", 7, 4);
  29.    private Command cancelEditCommand = new Command("Back", 2, 2);
  30.    private Command okEditCommand = new Command("OK", 4, 1);
  31.    private Command aboutCommand = new Command("Help", 5, 3);
  32.    private Command precisionHistCommand = new Command("Precision", 1, 5);
  33.    private Command cancelPrecCommand = new Command("Back", 2, 2);
  34.    private Command okPrecCommand = new Command("OK", 4, 1);
  35.  
  36.    public Eval() {
  37.       this.directions.setTimeout(-2);
  38.       this.directions.setString("Eval v" + getVersion() + " " + "Supported operators are: + - / * ( ) =, functions: sin(), cos(), constants: Pi.\nTo store result in a variable use '=' operator, for example: var1=sin(pi/4)*sin(pi/4)+cos(pi/4)*cos(pi/4)\nMidlet developed by WAP INDUSTRIAL [support@wapindustrial.com]. We can develop customized midlets for you!");
  39.    }
  40.  
  41.    private void about() {
  42.       Display.getDisplay(this).setCurrent(this.directions);
  43.    }
  44.  
  45.    public void commandAction(Command var1, Displayable var2) {
  46.       if (var1 == List.SELECT_COMMAND) {
  47.          this.histIndex = this.hist.getSelectedIndex();
  48.          String var3 = this.hist.getString(this.histIndex);
  49.          int var4 = var3.indexOf("\n=");
  50.          if (var4 >= 0) {
  51.             var3 = var3.substring(0, var4);
  52.          }
  53.  
  54.          this.entry.setString(var3);
  55.          this.display.setCurrent(this.entry);
  56.       } else if (var1 == this.precisionHistCommand) {
  57.          this.prec.setString(Integer.toString(MyDecimal.getPrecision()));
  58.          this.display.setCurrent(this.prec);
  59.       } else if (var1 == this.cancelPrecCommand) {
  60.          this.display.setCurrent(this.hist);
  61.       } else if (var1 == this.okPrecCommand) {
  62.          int var8 = 0;
  63.  
  64.          try {
  65.             var8 = Integer.parseInt(this.prec.getString());
  66.          } catch (NumberFormatException var7) {
  67.          }
  68.  
  69.          if (var8 > 0) {
  70.             MyDecimal.setPrecision(var8);
  71.          }
  72.  
  73.          this.display.setCurrent(this.hist);
  74.       } else if (var1 != this.okEditCommand && var1 != this.cancelEditCommand) {
  75.          if (var1 == this.exitCommand) {
  76.             this.destroyApp(false);
  77.             ((MIDlet)this).notifyDestroyed();
  78.          } else if (var1 == this.aboutCommand) {
  79.             this.about();
  80.          }
  81.       } else {
  82.          if (var1 == this.okEditCommand) {
  83.             String var9 = this.entry.getString();
  84.             String var11 = this.hist.getString(this.histIndex);
  85.             if (var11.compareTo("\n=press the SELECT button=") == 0) {
  86.                this.hist.delete(this.histIndex);
  87.             }
  88.  
  89.             try {
  90.                Result var5 = Result.Evaluate(var9);
  91.                var9 = var9 + "\n=" + var5.toString();
  92.             } catch (BadFormulaException var6) {
  93.                var9 = var9 + "\n=" + ((Throwable)var6).getMessage();
  94.             }
  95.  
  96.             if (var9.compareTo(var11) != 0) {
  97.                this.hist.insert(0, var9, (Image)null);
  98.                if (this.hist.size() > HISTORY_SIZE) {
  99.                   this.hist.delete(HISTORY_SIZE - 1);
  100.                }
  101.             }
  102.          }
  103.  
  104.          this.hist.setSelectedIndex(this.histIndex, true);
  105.          this.display.setCurrent(this.hist);
  106.       }
  107.  
  108.    }
  109.  
  110.    public void destroyApp(boolean var1) {
  111.       this.pauseApp();
  112.    }
  113.  
  114.    private int getInt(byte[] var1, int var2) {
  115.       return (var1[var2] & 255) << 24 | (var1[var2 + 1] & 255) << 16 | (var1[var2 + 2] & 255) << 8 | var1[var2 + 3] & 255;
  116.    }
  117.  
  118.    public static String getVersion() {
  119.       String var0 = Integer.toString(0);
  120.       var0 = var0 + "." + Integer.toString(4);
  121.       var0 = var0 + "." + Integer.toString(2);
  122.       return var0;
  123.    }
  124.  
  125.    public void pauseApp() {
  126.       this.writeProfile();
  127.       this.display.setCurrent((Displayable)null);
  128.       this.entry = null;
  129.       this.hist = null;
  130.    }
  131.  
  132.    private void putInt(byte[] var1, int var2, int var3) {
  133.       var1[var2] = (byte)(var3 >> 24 & 255);
  134.       var1[var2 + 1] = (byte)(var3 >> 16 & 255);
  135.       var1[var2 + 2] = (byte)(var3 >> 8 & 255);
  136.       var1[var2 + 3] = (byte)(var3 & 255);
  137.    }
  138.  
  139.    private void readProfile() {
  140.       RecordStore var1 = null;
  141.       byte[] var2 = new byte[512];
  142.  
  143.       try {
  144.          var1 = RecordStore.openRecordStore("eval", true);
  145.          int var3 = var1.getRecord(1, var2, 0);
  146.          if (var3 != 16) {
  147.             throw new Exception();
  148.          }
  149.  
  150.          if (this.getInt(var2, 0) != 1026) {
  151.             throw new Exception();
  152.          }
  153.  
  154.          MyDecimal.setPrecision(this.getInt(var2, 4));
  155.          int var4 = this.getInt(var2, 8);
  156.          int var5 = this.getInt(var2, 12);
  157.          int var6 = 2;
  158.  
  159.          for(int var7 = 0; var7 < var4; ++var7) {
  160.             int var8 = var1.getRecord(var6++, var2, 0);
  161.             String var9 = new String(var2, 0, var8);
  162.             this.hist.append(var9, (Image)null);
  163.          }
  164.  
  165.          for(int var15 = 0; var15 < var5; ++var15) {
  166.             int var16 = var1.getRecord(var6++, var2, 0);
  167.             int var10 = this.getInt(var2, 0);
  168.             String var11 = new String(var2, 4, var10);
  169.             var16 -= 4 + var10;
  170.             String var12 = new String(var2, 4 + var10, var16);
  171.             Result.addVar(new Variable(var11, new Result(new MyDecimal(var12)), false));
  172.          }
  173.       } catch (Exception var14) {
  174.       }
  175.  
  176.       if (var1 != null) {
  177.          try {
  178.             var1.closeRecordStore();
  179.          } catch (Exception var13) {
  180.          }
  181.       }
  182.  
  183.       if (this.hist.size() <= 0) {
  184.          this.hist.append("\n=press the SELECT button=", (Image)null);
  185.       }
  186.  
  187.    }
  188.  
  189.    public void startApp() {
  190.       MyDecimal.setPrecision(PRECISION);
  191.       this.display = Display.getDisplay(this);
  192.       this.entry = new TextBox("Type Expression", (String)null, 512, 0);
  193.       this.prec = new TextBox("digits after point", (String)null, 2, 2);
  194.       this.hist = new List("Select Expession", 3);
  195.       Result.addVar(new Variable("pi", new Result(MyDecimal.PI()), true));
  196.       this.readProfile();
  197.       this.hist.addCommand(this.exitCommand);
  198.       this.hist.addCommand(this.aboutCommand);
  199.       this.hist.addCommand(this.precisionHistCommand);
  200.       this.hist.setCommandListener(this);
  201.       this.entry.addCommand(this.cancelEditCommand);
  202.       this.entry.addCommand(this.okEditCommand);
  203.       this.entry.setCommandListener(this);
  204.       this.prec.addCommand(this.cancelPrecCommand);
  205.       this.prec.addCommand(this.okPrecCommand);
  206.       this.prec.setCommandListener(this);
  207.       this.display.setCurrent(this.hist);
  208.    }
  209.  
  210.    private void writeProfile() {
  211.       try {
  212.          int var2 = 0;
  213.          int var3 = Result.numberOfVars(false);
  214.          RecordStore var1 = RecordStore.openRecordStore("eval", true);
  215.          byte[] var4 = new byte[512];
  216.          this.putInt(var4, 0, 1026);
  217.          this.putInt(var4, 4, MyDecimal.getPrecision());
  218.          this.putInt(var4, 8, this.hist.size());
  219.          this.putInt(var4, 12, var3);
  220.  
  221.          try {
  222.             var1.setRecord(1, var4, 0, 16);
  223.          } catch (RecordStoreException var12) {
  224.             var1.addRecord(var4, 0, 16);
  225.          }
  226.  
  227.          ++var2;
  228.  
  229.          for(int var5 = 0; var5 < this.hist.size(); ++var5) {
  230.             String var6 = this.hist.getString(var5);
  231.             byte[] var7 = var6.getBytes();
  232.  
  233.             try {
  234.                var1.setRecord(var2 + 1, var7, 0, var6.length());
  235.             } catch (RecordStoreException var11) {
  236.                var1.addRecord(var7, 0, var6.length());
  237.             }
  238.  
  239.             ++var2;
  240.          }
  241.  
  242.          for(int var15 = 0; var15 < Result.vars.size(); ++var15) {
  243.             int var16 = 0;
  244.             Variable var8 = (Variable)Result.vars.elementAt(var15);
  245.             if (!var8.persistent) {
  246.                this.putInt(var4, 0, var8.name.length());
  247.                var16 += 4;
  248.                System.arraycopy(var8.name.getBytes(), 0, var4, 4, var8.name.length());
  249.                var16 += var8.name.length();
  250.                String var9 = var8.val.toString();
  251.                System.arraycopy(var9.getBytes(), 0, var4, var16, var9.length());
  252.                var16 += var9.length();
  253.  
  254.                try {
  255.                   var1.setRecord(var2 + 1, var4, 0, var16);
  256.                } catch (RecordStoreException var10) {
  257.                   var1.addRecord(var4, 0, var16);
  258.                }
  259.  
  260.                ++var2;
  261.             }
  262.          }
  263.       } catch (RecordStoreException var13) {
  264.          ((Throwable)var13).printStackTrace();
  265.       }
  266.  
  267.    }
  268. }
  269.